package zhoug.demo.chart2.chart;

import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import zhoug.chart.MPChartColors;
import zhoug.chart.MPChartConfig;
import zhoug.chart.charts.MPPieChart;
import zhoug.chart.core.styleconfig.IMPPieStyleConfig;
import zhoug.chart.data.HkChartData;
import zhoug.chart.data.MPLegendEntry;
import zhoug.chart.data.OnDateSetCreateListener;
import zhoug.chart.formatters.MPPieDataSetValueFormatter;
import zhoug.chart.markers.MPPieMarker;
import zhoug.chart.utils.MPChartUtils;
import zhoug.chart.views.MPLegendViewGroup;
import zhoug.demo.chart2.BaseActivity;
import zhoug.demo.chart2.DataGenerator;
import zhoug.demo.chart2.R;
import zhoug.common.http.SchedulerUtils;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.Disposable;


/**
 * 饼图
 *
 * @Author: zhoug
 * @Date: 2024-02-07
 * @Description:
 */
public class PieChartActivity extends BaseActivity {
    private static final String TAG = ">>>PieChartActivity";
    private static final int[] colors = {MPChartColors.COLOR_RED, MPChartColors.COLOR_BLUE, MPChartColors.COLOR_GREEN,
            MPChartColors.COLOR_SOIL, MPChartColors.COLOR_DEEP_PURPLE, MPChartColors.COLOR_LAVENDER, MPChartColors.COLOR_GRASS_GREEN,
            MPChartColors.COLOR_YELLOW, MPChartColors.COLOR_ORANGE
    };
    private boolean mSliceSpace;
    private boolean mZeroData;

    private Button mBtnSetting;
    private MPPieChart mPieChart1;
    private MPPieChart mPieChart2;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private Switch mSwitchSliceSpace;
    private Switch mSwitchZero;
    private MPLegendViewGroup mLegendViewGroup;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_piechart;
    }

    @Override
    protected void findViews() {
        mBtnSetting = findViewById(R.id.btn_setting);
        mPieChart1 = findViewById(R.id.pieChart1);
        mPieChart2 = findViewById(R.id.pieChart2);
        mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        mSwitchSliceSpace = findViewById(R.id.SwitchSliceSpace);
        mSwitchZero = findViewById(R.id.SwitchZero);
        mLegendViewGroup = findViewById(R.id.LegendViewGroup);
    }

    @Override
    protected void init() {
        mPieChart1.getLegend().setEnabled(false);
        mPieChart1.setDrawHoleEnabled(false);
        mPieChart1.setOnDateSetCreateListener(new OnDateSetCreateListener<PieDataSet>() {
            @Override
            public void onInitDataSetStyle(PieDataSet dataSet, int index) {
                dataSet.setSliceSpace(mSliceSpace ? MPChartConfig.PIE_SLICE_SPACE : 0);
            }
        });
        mPieChart2.setOnDateSetCreateListener(new OnDateSetCreateListener<PieDataSet>() {
            @Override
            public void onInitDataSetStyle(PieDataSet dataSet, int index) {
                dataSet.setSliceSpace(mSliceSpace ? MPChartConfig.PIE_SLICE_SPACE : 0);
            }
        });
        MPPieDataSetValueFormatter pieDataSetValueFormatter = new MPPieDataSetValueFormatter(mPieChart1);
        pieDataSetValueFormatter.setShowValue(true);
        pieDataSetValueFormatter.setShowPercent(true);
        mPieChart1.setDataSetValueFormatter(pieDataSetValueFormatter);


        pieDataSetValueFormatter = new MPPieDataSetValueFormatter(mPieChart2);
        pieDataSetValueFormatter.setShowValue(true);
        pieDataSetValueFormatter.setShowPercent(true);
        mPieChart2.setDataSetValueFormatter(pieDataSetValueFormatter);


        MPPieMarker pieMarker = new MPPieMarker(this);
        pieMarker.showSum("违规总数",true);
        pieMarker.setShowType(MPPieMarker.SHOW_TYPE_All);
        pieMarker.showPercent(true);
        mPieChart1.setMarker(pieMarker);
        pieMarker = new MPPieMarker(this);
        pieMarker.showPercent(true);
        pieMarker.setShowType(MPPieMarker.SHOW_TYPE_HIGHLIGHT);
        mPieChart2.setMarker(pieMarker);
        mPieChart2.setDrawCenterText(true);
        mPieChart2.setCenterTextColor(MPChartConfig.LEGEND_TEXT_COLOR);
        mSwipeRefreshLayout.setOnRefreshListener(this::loadData);
        mBtnSetting.setOnClickListener(v -> {
            setting();
            loadData();
        });
        mSwitchSliceSpace.setOnCheckedChangeListener(onCheckedChangeListener);
        mSwitchZero.setOnCheckedChangeListener(onCheckedChangeListener);

        mPieChart1.setMinValuePercentCanDraw(0);
        mPieChart2.setMinValuePercentCanDraw(0);
        setting();
        loadData();
    }

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = (buttonView, isChecked) -> {
        setting();
        loadData();
    };

    private void setting() {
        IMPPieStyleConfig styleConfig = mPieChart1.getStyleConfig();
        styleConfig.setPieColors(colors);
        styleConfig = mPieChart2.getStyleConfig();
        styleConfig.setPieColors(colors);

        mSliceSpace = mSwitchSliceSpace.isChecked();
        mZeroData = mSwitchZero.isChecked();

    }

    private void loadData() {
        Disposable subscribe = Observable
                .create((ObservableOnSubscribe<List<HkChartData>>) emitter -> {
                    Thread.sleep(200);
                    Random random = new SecureRandom();
                    List<HkChartData> list = new ArrayList<>();
                    String[] aiIllegal = DataGenerator.AI_ILLEGAL;
                    for (int i = 0; i < aiIllegal.length; i++) {
                        HkChartData chartData = new HkChartData();
                        chartData.setName(aiIllegal[i]);
                        if(mZeroData){
                            chartData.put("数量",0);
                        }else{
                            if(i==2 || i==4){
                                chartData.put("数量",0);
                            }else{
                                chartData.put("数量", random.nextInt(100));
                            }
                        }
                        list.add(chartData);
                    }
                    emitter.onNext(list);
                    emitter.onComplete();

                })
                .compose(SchedulerUtils.apply())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(data -> {
                    mPieChart1.setHkChartData(data);
                    mPieChart2.setHkChartData(data);
                    PieData pieData = mPieChart2.getData();
                    int yValueSum=0;
                    if(pieData!=null){
                         yValueSum = (int) pieData .getYValueSum();
                    }
                    mPieChart2.setCenterText(String.valueOf(yValueSum));
                    List<MPLegendEntry> mpLegendEntries = MPChartUtils.computeLegend(mPieChart1.getData(), null);
                    mLegendViewGroup.setData(mpLegendEntries);
                    mLegendViewGroup.notifyDataSetChanged();
                }, throwable -> {
                    mSwipeRefreshLayout.setRefreshing(false);
                    throwable.printStackTrace();
                    mPieChart1.error();
                    mPieChart2.error();

                });


    }


}